<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends MY_Controller {
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		$user = $this->session->userdata('rsh_user');
		if(!isset($user['user_id']) || !$user['user_id']){
			$str = $_SERVER['REQUEST_URI'];
			$this->js->go("/login/index.html");
			exit;
		}
		
		$this->tab = 'user';
		$this->sub = 'account';
	}

	

	//宝付充值
	public function zhs_recharge(){
		$act = $this->input->post('act');
		$user = $this->session->userdata('rsh_user');
		$this->load->helper('captcha');


		$data = $this->get_user_info('account_recharge','account_info');
		$this->load->model('User_model', 'user');
		$this->load->model('Payment_model', 'payment');
		$this->load->model('Accountrecharge_model', 'recharge');
		$data['payment_list0']=$this->payment->get_list(0);
		$data['var_payment_bank'] 		= $this->payment->arrays('var_payment_bank');

		if($act == 'submit'){
			$msg = '';
			$time = date("Ymdhis");
			// 20150425
			$type = 1;//只允许网上充值
			$money = $this->input->post('money');
			$captcha = $this->input->post('captcha');
			$payID = $this->input->post('PayID');
			if($captcha == ''){
				$msg = '验证码不能为空';
			}elseif ($payID == ''){
				$msg = '支付方式不能为空';
			}elseif ($captcha!=$_SESSION['captcha']){
				$msg = '验证码不正确';
			}elseif(!is_numeric($money)){
				$msg = '无效的金额';
			}else{
				$money = round($money,2);	
				if($money < 0){
					$msg = '无效的金额';
				}else{
					if($type == '1'){//网上充值
						$bank_fun = '';
						$bank_code = '';
						$bank_str = $this->input->post('bankRadio');
						$payment1 = $this->input->post('payment1');
						if((isset($bank_str) && $bank_str != '') && (isset($payment1) && $payment1!='')){
							$msg = '只能选择一种支付方式';
						}
						if (isset($bank_str) && $bank_str != ''){
							$bank = explode("|",$bank_str);
							$payment = $bank[1];
							$bank_code = $bank[0];
							$bank_fun = $bank[2];
						}elseif(isset($payment1) && $payment1!=''){
							$payment = $payment1;
						}else{
							$msg = '请选择银行卡或者第三方支付平台';
						}
						$remark = $this->input->post('payname'.$payment). '在线充值';
						$timetmp = strtotime('2013-10-01 00:00:00');
						$fee = 0.0;
						
					}else{
						$msg = '无效的充值方式';
					}
					if($msg==''){
						$post_time = time();
						$recharge = array(
							'user_id' => $user['user_id'],
							'trade_no' => $post_time.$user['user_id'].rand(1,9),
							'fstatus' => 0,
							'money' => $money,
							'payment' => $payment,
							'freturn' => '',
							'ftype' => $type,
							'remark' => $remark,
							'fee' => $fee.'',
							'verify_userid' => 0,
							'verify_time' => $post_time,
							'verify_ip' => '',
							'verify_remark' => '',
							'add_time' => $post_time,
							'addip' => $this->input->ip_address()
						);
						$res = $this->recharge->add($recharge);
						
						if($res > 0){
							if($type == 1){//网上充值
								/** 20150424 修改*/
								$MARK = "|";
								$dataRecharge = Array();
								$dataRecharge['MemberID']		=	MERCHANT_ID;//商户号
								$dataRecharge['TerminalID'] 	= 	TERMINAL_ID; // 终端号
								$dataRecharge['TransID']		=	$recharge['trade_no'];//流水号
								$dataRecharge['PayID']			=	$payID;//支付方式
								$dataRecharge['TradeDate']		=	$time;//交易时间
								$dataRecharge['OrderMoney']		=	$recharge['money']*100;//订单金额,单位分*100 = 元
								$dataRecharge['ProductName']	=	"";//产品名称
								$dataRecharge['Amount']			=	1;//商品数量
								$dataRecharge['Username']		=	$user['user_id'];//支付用户名
								$dataRecharge['AdditionalInfo']	=	"";//订单附加消息
								$dataRecharge['PageUrl']		=	PageUrl;//通知商户页面端地址
								$dataRecharge['ReturnUrl']		=	ReturnUrl;//服务器底层通知地址
								$dataRecharge['NoticeType']		=	1;//通知类型	
								$dataRecharge['Md5key']			=	KEY;//md5密钥（KEY）
								//MD5签名格式
								$dataRecharge['Signature']		= md5(  $dataRecharge['MemberID'].
																		$MARK.$dataRecharge['PayID'].
																		$MARK.$dataRecharge['TradeDate'].
																		$MARK.$dataRecharge['TransID'].
																		$MARK.$dataRecharge['OrderMoney'].
																		$MARK.$dataRecharge['PageUrl'].
																		$MARK.$dataRecharge['ReturnUrl'].
																		$MARK.$dataRecharge['NoticeType'].
																		$MARK.$dataRecharge['Md5key']
																 );
								if(TESTMODE == 1){//测试接口
									$dataRecharge['payUrl']			="http://vgw.baofoo.com/payindex";//借贷混合
								}else{//正式环境
									$dataRecharge['payUrl']			="https://gw.baofoo.com/payindex";//借贷混合
								}
								$dataRecharge['InterfaceVersion']	= "4.0";
								$dataRecharge['KeyType'] 			= "1";
								// post 提交充值
								$this->load->view('account/zhs_accountToRecharge',$dataRecharge);
							}elseif($type == 2){//用户线下充值
								$msg = '充值已提交，请等待审核';
							}
						}else{
							$msg = '提交失败';
						}
					}
				}
			}
			$data['msg'] = $msg;
		}
			$parms['page_start'] = intval($this->input->get('per_page'));
			$parms['page_size'] = 10;
			$parms['where']['user_id'] = $user['user_id'];
			$resData = $this->recharge->get_accountrecharge_list($parms);
			$data['list'] = $resData['accountrecharge_list'];
			$data['recharge_status'] 	= $this->recharge->arrays("account_recharge_status");
			$data['recharge_type'] 	= $this->recharge->arrays("account_recharge_type");
			$data['payment_list'] = $this->payment->get_list();
			$data['group_total'] = $this->recharge->get_recharge_group_sum($user['user_id']);
			$this->load->library('pagination');
			$config['base_url'] = '/account/zhs_recharge/index.html?';
			$config['total_rows'] = $resData['accountrecharge_count'];
			$config['per_page'] = 20;
			$config['first_link'] = '首页';
			$config['next_link'] = '下一页';
			$config['prev_link'] = '上一页';
			$config['last_link'] = '尾页';
			$this->pagination->initialize($config);
			$data['user_sub'] = 'tender_acnt';
			$data['sub'] = 'tender';
			$data['sub_1'] = "会员中心";
			$data['sub_1_href'] = "/user/zhs_center.html";
			$data['sub_2'] = "资金记录";
			$data['sub_2_href'] = "/account/zhs_details.html";
			$data['sub_3'] = "账户充值";
			$data['sub_3_href'] = "/account/zhs_recharge";
			$data['title'] = "账户充值".' - '.CON_KEYWORDS;
			$this->load->view('account/zhs_account_recharge',$data);		
		
	}
	
	//宝付充值日志
	function recharge_log(){
		$user = $this->session->userdata('rsh_user');
		$data = array();
		$parms['page_start'] = intval($this->input->get('per_page'));
		$parms['page_size'] = 20;
		$parms['where']['user_id'] = $user['user_id'];
		$this->load->model('Accountrecharge_model', 'recharge');
		$this->load->model('Payment_model', 'payment');
		//充值统计
		$data = $this->get_user_info('account_recharge_log','account_info');
		$resData = $this->recharge->get_accountrecharge_list($parms);
		$data['list'] = $resData['accountrecharge_list'];
		$data['recharge_status'] 	= $this->recharge->arrays("account_recharge_status");
		$data['recharge_type'] 	= $this->recharge->arrays("account_recharge_type");
		$data['payment_list'] = $this->payment->get_list();
		$data['group_total'] = $this->recharge->get_recharge_group_sum($user['user_id']);
		$this->load->library('pagination');
		$config['base_url'] = '/account/recharge_log/index.html?';
		$config['total_rows'] = $resData['accountrecharge_count'];
		$config['per_page'] = 20;
		$config['first_link'] = '首页';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$config['last_link'] = '尾页';
		$this->pagination->initialize($config);

		$data['title'] = "充值记录".' - '.CON_KEYWORDS;
		$this->load->view('/account/account_recharge_log', $data);
	}
	

	//丰付充值
	public function zhs_recharge_f(){
		$act = $this->input->post('act_f');
		$user = $this->session->userdata('rsh_user');
		$this->load->helper('captcha');

		$data = $this->get_user_info('account_recharge','account_info');
		$this->load->model('User_model', 'user');

		if($act == 'submit1'){

			$msg = '';
			$time = date("Ymdhis");

			$type = 1;//只允许网上充值
			$money_f = $this->input->post('money_f');
			$captcha = $this->input->post('captcha_f');
			if($captcha == ''){
				$msg = '验证码不能为空';
			}elseif ($captcha!=$_SESSION['captcha']){
				$msg = '验证码不正确';
			}elseif(!is_numeric($money_f)){
				$msg = '无效的金额';
			}else{
				$msg='success';
			}
			$data['msg']=$msg;
		}
		echo $data['msg'];
		$this->load->view('account/zhs_account_recharge_f',$data);
				
	}
	//丰付充值日记
	public function recharge_log_f(){

	}
	
	
	//绑定银行卡
	function zhs_bank(){
		$user = $this->session->userdata('rsh_user');
		
		//echo $user['realname1'];exit;
		
		$typer = $this->input->post('typer');
		$this->load->model('Account_bank_model', 'bank');
		$msg = '';
		$this->load->helper('captcha');
		$data = $this->get_user_info('account_bank','bank');
		$data['bank_list'] = $this->bank->arrays('var_account_bank');
		$bank = $this->bank->get_by_uid($user['user_id']);
		$this->load->model('Area_model', 'area');
		$data['area'] = $this->area->get_list();
		if($typer == 'add'){
			if(count($bank)>=2 && $typer != 'update'){
				echo '银行账户只允许认证两张银行卡';
				exit;
			}else{
				$verify_code = $this->input->post('ve_code');
				
				$setArr = array(
					'user_id' => $user['user_id'],
					'account' => $this->input->post('account'),
					'bank' => $this->input->post('bank'),
					'province' => $this->input->post('province'),
					'city' => $this->input->post('city'),
					'area' => $this->input->post('area'),
					'branch' => $this->input->post('branch'),
					'add_time' => time(),
					'addip' => $this->input->ip_address(),
				); 
				$bank_id = $this->input->post('card_id');
				if(((!isset($bank[0])) || ($bank[0]['default_flag'] != 1))&& !$this->input->post('default_flag') ) $default_flag = 1;
				if($this->input->post('default_flag') || $default_flag){
					$setArr['default_flag'] = $this->input->post('default_flag')?$this->input->post('default_flag'):$default_flag;
					
					$where = array('user_id' => $user['user_id']);
					$this->bank->update(array('default_flag'=>'0'),$where);			
				}
				
				foreach ($setArr as $v){
					if(!$v || $v==''){
						echo '银行账户信息请勿为空';
						exit; 
					}
				}
				
				/*if($typer == 'update'){
					$where = array('id'=>$bank_id,'user_id' => $user['user_id']);
					$id = $this->bank->update($setArr,$where);
				}
			
				$this->load->library('baofooSdk/baofooRequestService',null,'br');
				$config['user_id'] = $user['user_id'];
				$config['type'] = "1";// 0删除1新增
				$config['bank_no'] = $setArr['account'];
				$config['pro_value'] = $data['area'][$setArr['province']];
				$config['city_value'] = $data['area'][$setArr['city']];
				$config['bank_name'] = $data['bank_list'][$setArr['bank']];
				$config['bank_address'] = $setArr['branch'];
				$config['validate_code'] = $verify_code;
				$this->load->library('baofooSdk/dto/request/bindCardDto',$config,'bindDto');
				$res = $this->br->api_BindBankCard($this->bindDto);
					*/
				$res['code'] = 'CSD000';
				if($res['code'] == 'CSD000') {
					$id = $this->bank->add($setArr);
					//发放代金券
					$this->load->model('Redmanagement_model', 'redmanagement');
					$this->redmanagement->send_red($user['user_id'],4,"银行卡绑定");
					if($id>0){
						echo '';
						exit;
					}else{
						echo '保存失败';
						exit;
					}
				}
				else echo '保存失败:'.$res['msg'];
				
			}
		}else{
			for($i=0; $i<count($bank); $i++){
				$data['bank'.$i] = $bank[$i];
			}
			$this->load->model('User_model');
			$us = $this->User_model->get_realname_by_id($user['user_id']);
			$data['real_status'] = $us['real_status'];
			// 页面组信息
			$data['title'] = "银行账户".' - '.CON_KEYWORDS;
			$data['sub'] = "user";
			$data['user_sub'] = "user_bank";
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "银行卡管理";
			$data['sub_2_href'] = "/user/zhs_bank.html";
			$this->load->view('account/zhs_account_bank', $data);
		}
	}
	
	//绑定银行卡删除操作
	function zhs_delete_bank(){
		$id = $this->input->post('bank_id');
		$ve_code = $this->input->post('ve_code');
				
		$usermsg = $this->session->userdata('rsh_user');
		$user_id = $usermsg['user_id'];
		$this->load->model('User_model', 'user');
		$usermsg = $this->user->get_by_uid($user_id);
		$session_code = $this->session->userdata ( $usermsg ['phone'] );

		if($id > 0){
			$user = $this->session->userdata('rsh_user');
			$this->load->model('Account_bank_model', 'bank');
			$bank = $this->bank->get_one(array('id'=>$id,'user_id'=>$user['user_id']));
			if(isset($bank) ){
				$this->load->model('Area_model', 'area');
				$area = $this->area->get_list();
				$bank_list = $this->bank->arrays('var_account_bank');
				/***
				$this->load->library('baofooSdk/baofooRequestService',null,'br');
				$config['user_id'] = $user['user_id'];
				$config['type'] = "0";// 0删除1新增
				$config['bank_no'] = $bank['account'];
				$config['pro_value'] = $area[$bank['province']];
				$config['city_value'] = $area[$bank['city']];
				$config['bank_name'] = $bank_list[$bank['bank']];
				$config['bank_address'] = $bank['branch'];
				$config['validate_code'] = $ve_code;
				$this->load->library('baofooSdk/dto/request/bindCardDto',$config,'bindDto');
				$res = $this->br->api_BindBankCard($this->bindDto);
				'CSD000'
				*/
				$res['code'] = 'CSD000';
				if($res['code'] == 'CSD000' && $session_code == $ve_code) {
					 $this->bank->update(array('default_flag'=>'1'),array('user_id'=>$user['user_id'],'is_removal'=>'0'));
					$res = $this->bank->delete($id,array('is_removal'=>'1'));
					if($res > 0) echo '';
					else echo '操作失败';
				}
				else echo '删除失败,手机验证码错误';
			}
			else echo '操作失败:此卡不存在';
		}
	}
	
	//提现
	function zhs_cash(){
		$act = $this->input->post('act');
		$user = $this->session->userdata('rsh_user');
		$this->load->helper('captcha');
		$this->load->model('User_model', 'user');
		$this->load->model('Account_bank_model', 'bank');
		$this->load->model('Account_model', 'account');
		$this->load->model('Accountlog_model', 'accountlog');
		$this->load->model('Area_model', 'area');
		$data = $this->get_user_info('account_cash','account_info');
		$data['area'] = $this->area->get_list();//获取银行归属地区信息
		$data['bank_list'] = $this->bank->arrays('var_account_bank');//获取银行列表
		$data['user'] = $this->user->get_by_uid($user['user_id']);//获取用户信息
		$data['account'] = $this->account->get_account($user['user_id']);//获取用户资金信息
		$bank = $this->bank->get_by_uid($user['user_id']);//取到提现用户的绑定银行账号
		for($i=0; $i<count($bank); $i++){
			$data['bank'.$i] = $bank[$i];
		}
		$this->load->model('Borrow_model');
		$data['invite_total'] = $this->Borrow_model->tender_ing_total($user['user_id']);//正在理财中金额
		if(!isset($data['invite_total'])) $data['invite_total'] = 0;
		//15天之内的充值金额提现需要收手续费 0.01收取
		$dont_moeny = $this->accountlog->donnot_mention_15($user['user_id']);
		if(!isset($dont_moeny)) $dont_moeny = 0;
		if($act == 'submit'){
			if($data['user']['real_status'] != 1){
				$this->js->alert('请先通过实名认证');
				$this->js->go('/user/realname.html');
				exit;
			}if($data['user']['phone_status'] != 1){
				$this->js->alert('请先通过手机认证');
				$this->js->go('/user/phone.html');
				exit;
			}elseif($data['user']['paypassword'] == ''){
				$this->js->alert('请先设置交易密码');
				$this->js->go('/user/paypwd.html');
				exit;
			}elseif (!isset($data['bank0'])){
				$this->js->alert('请先绑定提现账号');
				$this->js->go('/account/bank.html');
				exit;
			}else{
				$verify_code = $this->input->post('verify');
				$paypwd = $this->input->post('paypwd');
				$money  = $this->input->post('money');
				$bank_id = $this->input->post('bank_id');
				if($verify_code == ''){
					$this->js->alert('验证码不能为空');
					exit;
				}
				$code = $this->session->userdata($user ['phone']);
				if ($verify_code!=$code){
					$this->js->alert('验证码不正确');
					exit;
				}
				if(!is_numeric($money)){
					$this->js->alert('无效金额');
					exit;
				}
				$money = round($money,2);
				if($money < 50){
					$this->js->alert('单笔提现最低金额50元');
					exit;
				}
				if($money > 500000){
					$this->js->alert('单笔提现最高金额500000元');
					exit;
				}
				if($bank_id == $bank[0]['id']){
					$data['bank'] = $data['bank0'];
				}
				elseif ($bank_id == $bank[1]['id']){
					$data['bank'] = $data['bank1'];
				}
				else {
					$this->js->alert('请选择正确的绑定银行账号');
					exit;
				}
				if(md5(md5($paypwd)) == $data['user']['paypassword']){
					$account_result =  $data['account'];
					if($account_result['use_money'] < $money){
						$this->js->alert('提现金额不能超过可用金额');
						exit;
					}else{
						$use_money = ($account_result['use_money'] + $data['invite_total']) - $money;
						$use_money = round($use_money,2);
						$dont_moeny = round($dont_moeny,2);
						$time = time();
						$ip = $this->input->ip_address();
						$fee = 0;
						
						//提现:15天之内的金额需要收手续费，按千分之五
						if($data['user']['is_loan']==0 && $use_money < $dont_moeny){
							$cha = $dont_moeny-$use_money;
							$cha = min($cha,$money);
							$fee = sprintf("%.2f", ($cha*0.005*100)/100);
						}
						//防止重复提交订单
						$local_smscode = trim($this->input->post("smscode"));
						$smscode = $this->session->userdata('smscode');
						if($local_smscode != $smscode) { $this->js->alert('请求已经接收，请刷新再试'); exit;}
						else $this->session->unset_userdata('smscode');
						
						//添加提现表记录
						$setArr = array(
							'user_id' => $user['user_id'],
							'fstatus' => '0',
							'account' => $data['bank']['account'],
							'bank' => $data['bank']['bank'],
							'branch' => $data['bank']['branch'],
							'province' => $data['bank']['province'],
							'city' => $data['bank']['city'],
							'total' => $money,
							'credited' => $money - $fee,
							'fee' =>$fee,
							'add_time' => $time,
							'addip' => $ip
						);
						$this->load->model('Accountcash_model', 'cash');
						$ret = $this->cash->add($setArr);
						//添加提现冻结资金日志
						
						if($ret > 0){
							$log = array(
								'user_id' => $user['user_id'],
								'ftype' => CASH_FROST,
								'money' => $money,
								'total' => $account_result['total'],
								'use_money' => $account_result['use_money']-$money,
								'no_use_money' => $account_result['no_use_money']+$money,
								'collection' => $account_result['collection'],
								'red_money' => $account_result['red_money'],
								'to_user' => 0,
								'add_time' => $time,
								'addip' => $ip,
								'remark' => "提现冻结"
							);
							$ret = $this->account->addlog($log);
						}
						
						if($ret){
							$this->send_msg(array('user_id'=>$user['user_id'],sys_id=>22,'money'=>$money));//发送内信
							$this->db->trans_commit();
							$this->js->alert('提现申请已提交，请等待审核');
							$this->js->go('/account/zhs_cash/index.html');
							exit;
						}else{
							$this->db->trans_rollback();
							$this->js->alert('操作失败，请重试');
							$this->js->go('/account/zhs_cash/index.html');
							exit;
						}
					}
				}else{
					$this->js->alert('交易密码不正确');
					$this->js->go('/account/zhs_cash/index.html');
					exit;
				}
				
			}
			
		}else{
		
			$this->load->model('Accountcash_model', 'cash');
			//防止重复提交
			$data['smscode'] = rand_string ( time(), 6, 3, 13 );
			$this->session->set_userdata ( 'smscode', $data['smscode']);
			
			$data['cash_fee']= $dont_moeny;//不需收手续费的金额
			$parms['page_start'] = intval($this->input->get('per_page'));
			$parms['page_size'] = 10;
			$parms['where']['user_id'] = $user['user_id'];
			$resData = $this->cash->get_accountcash_list($parms);
			$data['list'] = $resData['accountcash_list'];
			$data['sum_money'] = $this->cash->get_succ_total($user['user_id']);
			$data['cash_status'] 	= $this->cash->arrays("account_cash_status");
			$data['bank_list'] = $this->cash->arrays("account_bank");
			$this->load->library('pagination');
			$config['base_url'] = '/account/zhs_cash/index.html?';
			$config['total_rows'] = $resData['accountcash_count'];
			$config['per_page'] = 10;
			$config['first_link'] = '首页';
			$config['next_link'] = '下一页';
			$config['prev_link'] = '上一页';
			$config['last_link'] = '尾页';
			$this->pagination->initialize($config);
		
			$data['sub_1'] = "会员中心";
			$data['sub_1_href'] = "/user/zhs_center.html";
			$data['sub_2'] = "提现";
			$data['sub_2_href'] = "/acount/zhs_cash.html";
			$data['title'] = "账户提现".' - '.CON_KEYWORDS;
			$data['user_sub'] = "tender_cash";
			$data['sub'] = "tender";
			
			$this->load->view('account/zhs_account_cash', $data);
		}
	}
	
	
	function cashed(){
		$user = $this->session->userdata('rsh_user');
		$data = array();
		$parms['page_start'] = intval($this->input->get('per_page'));
		$parms['page_size'] = 10;
		$parms['where']['user_id'] = $user['user_id'];
		$this->load->model('Accountcash_model', 'cash');
		$data = $this->get_user_info('account_cashed','account_info');
		
		$resData = $this->cash->get_accountcash_list($parms);
		$data['list'] = $resData['accountcash_list'];
		$data['sum_money'] = $this->cash->get_succ_total($user['user_id']);
		$data['cash_status'] 	= $this->cash->arrays("account_cash_status");
		$data['bank_list'] = $this->cash->arrays("account_bank");
		$this->load->library('pagination');
		$config['base_url'] = '/account/cashed/index.html?';
		$config['total_rows'] = $resData['accountcash_count'];
		$config['per_page'] = 10;
		$config['first_link'] = '首页';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$config['last_link'] = '尾页';
		$this->pagination->initialize($config);
		
		
		$data['title'] = "提现记录".' - '.CON_KEYWORDS;
		$this->load->view('/account/account_cash_log', $data);
	}
	
	function zhs_cash_cancel($id=0){
		$ret = array('msg'=>'错误操作','url'=>'/account/zhs_cash.html');
		$id = intval($id);
		$user = $this->session->userdata('rsh_user');
		if($id<1 || !isset($user['user_id']) || !$user['user_id']){
			echo json_encode($ret);
			exit;
		}
		
		$this->load->model('Accountcash_model');
		$this->load->model('Account_model');
		$cash = $this->Accountcash_model->get_by_id($id);
		
		if($cash['user_id'] != $user['user_id']){
			echo json_encode($ret);
			exit;
		}
		if($cash['fstatus'] != 0){
			echo json_encode($ret);
			exit;
		}
		$this->db->trans_strict(FALSE);
		$this->db->trans_begin();//事务开始
		$setArr["fstatus"] = 3;
  		$result = $this->Accountcash_model->update($id, $setArr);
  		if($result){
  			$account = $this->Account_model->get_account($cash['user_id']);
  			$log = array(
  				'user_id' => $cash['user_id'],
  				'ftype' => CASH_CANCEL,//提现取消
  				'total' => $account['total'],
  				'money' => $cash['total'],
  				'use_money' => $account['use_money']+$cash['total'],
  				'no_use_money' => $account['no_use_money']-$cash['total'],
  				'collection' => $account['collection'],
  				'red_money' => $account['red_money'],
  				'to_user' => 1,
  				'remark' => '提现取消',
  				'add_time' => time(),
  				'addip' => $this->input->ip_address(),
  			);
  			$result = $this->Account_model->addlog($log);
  		}
  		
		if($this->db->trans_status() === TRUE){
			$this->db->trans_commit();
			$ret['msg']='操作成功';
			$ret['url']='';
			echo json_encode($ret);
			exit;
		}else{
			$this->db->trans_rollback();
			$ret['msg']='操作失败';
			$ret['url']='';
  			echo json_encode($ret);
			exit;
		}
		
		
	}
	
	// 资金记录
	function zhs_details(){
		$act = $this->input->get('act');
		if ($act == 'excel'){
			$this->detailsexcel();
		}else{
			$user = $this->session->userdata('rsh_user');
			$data = array();
			$parms['page_start'] = intval($this->input->get('per_page'));//intval($page);
			$parms['page_size'] = 20;
			$parms['where']['user_id'] = $user['user_id'];
			$parms['where']['strtime'] = $this->input->get('start_time');
			$parms['where']['endtime'] = $this->input->get('end_time');
			$parms['where']['ftype'] = $this->input->get('type');
			$this->load->model('Accountlog_model', 'accountlog');			
			
			$data = $this->get_user_info('account_detail','account_detail');
			$resData = $this->accountlog->get_accountlog_list($parms);
			
			$this->load->model('Accountcash_model', 'cash');
			$data['user_cash_all']  = $this->cash->get_user_cash_total($user['user_id'],4);
			$this->load->model('Accountrecharge_model', 'recharge');
			$data['user_recharge_all']  = $this->recharge->get_user_recharge_total($user['user_id'],1);	
			
			$data['list'] = $resData['accountlog_list'];
			
			//if(count($data['list']) < 1)$this->js->alert('查询无符合条件的数据');
			$data['log_type'] 	= $this->accountlog->arrays("account_type");
			$this->load->library('pagination');
			$config['base_url'] = '/account/zhs_details.html?start_time='.$parms['where']['strtime'].'&end_time='.$parms['where']['endtime'].'&type='.$parms['where']['ftype'];
			$config['total_rows'] = $resData['accountlog_count'];
			$config['per_page'] = 20;
			$config['first_link'] = '首页';
			$config['next_link'] = '下一页';
			$config['prev_link'] = '上一页';
			$config['last_link'] = '尾页';
			$this->pagination->initialize($config);
			$data['where'] = $parms['where'];
			$data['title'] = "资金明细".' - '.CON_KEYWORDS;
			$data['user_sub'] = 'tender_acnt';
			$data['sub'] = 'tender';
			
			$data['sub_1'] = "会员中心";
			$data['sub_1_href'] = "/user/zhs_center.html";
			$data['sub_2'] = "资金记录";
			$data['sub_2_href'] = "/account/zhs_details.html";
			
			$this->load->view('/account/zhs_account_log', $data);
		}
	}
	
	/*function detailsexcel(){
		$user = $this->session->userdata('rsh_user');
		$parms['where']['user_id'] = $user['user_id'];
		$parms['where']['strtime'] = $this->input->get('start_time');
		$parms['where']['endtime'] = $this->input->get('end_time');
		$parms['where']['ftype'] = $this->input->get('type');
		$parms['limit'] = 'all';
		$this->load->model('Accountlog_model', 'accountlog');
		$logType = $this->accountlog->arrays("account_type");
		$resData = $this->accountlog->get_accountlog_list($parms);
		$list = $resData['accountlog_list'];
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("资金明细")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("类型", "操作金额", "总金额", "可用金额", "冻结金额", "待收金额", "记录时间", "备注");
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		
		for($i = 0; $i < count($list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $logType[$list[$i]['ftype']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $list[$i]['money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $list[$i]['total']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $list[$i]['use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $list[$i]['no_use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $list[$i]['collection']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, date('Y-m-d H:i:s',$list[$i]['add_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $list[$i]['remark']);
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="资金明细'.date('Y-m-d H:i:s',time()).'.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}*/
	
	
	// *********** by liub 20141201 我的代金卷
	function getRedMoney(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Account_model', 'account');
		$this->load->model('Accountlog_model', 'accountlog');
		$where['where']['user_id'] = $user['user_id'];
		$where['where']['ftype'] = REDPACKAGE;
		$where['limit'] = 'all';
		$dat = $this->accountlog->get_accountlog_list($where);
		$data['accountlog_list0'] = $dat['accountlog_list'];//获取代金卷的日志数据
		
		$where['where']['ftype'] = REDPACKAGE_DEDUCT;
		$dat = $this->accountlog->get_accountlog_list($where);
		$accountlog_list1 = $dat['accountlog_list'];//获取使用代金卷的日志数据
		

		
		$accou_sum = 0;
		foreach($accountlog_list1 as $val){
			$accou_sum += $val['money'];
		}
		$data['accou_sum'] = $accou_sum;
		$res  = $this->account->get_account($user['user_id']);
		$data['red_money'] = $res['red_money'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/account/getRedMoney.html?';
		$config['total_rows'] = $data['accountlog_list0']['accountlog_count'];
		$config['per_page'] = 1; 
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		
		$data['user_sub'] = "user_redmoney";
		$data['sub'] = "user";
		
		
		$data['title'] = "账户详情".' - '.CON_KEYWORDS;
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "我的代金卷";
		$data['sub_2_href'] = "/account/getRedMoney.html";
		$this->load->view('/account/zhs_redacnt_info', $data);
	}
	//各银行充值限额
	function  bankxiane(){
		$this->load->view('/account/zhs_bank');
	}
}